const name = "Orders"
const lowerName = "orders"
const name_CN = "资助订单"
const fs = require('fs')

// 模板地址
const api_temp_path = __dirname+"/template/api/api.tempalet.txt"
const store_temp_path = __dirname+"/template/store/store.tempalet.txt"
const dialog_temp_path = __dirname+"/template/page/comp/Dialog.template.txt"
const search_temp_path = __dirname+"/template/page/comp/SearchForm.template.txt"
const data_temp_path = __dirname+"/template/page/data.template.txt"
const index_temp_path = __dirname+"/template/page/index.template.txt"

// 生成的文件路径
const api_new_path = __dirname+`/newFile/api/${lowerName}.js`
const store_new_path = __dirname+`/newFile/store/${lowerName}.js`
const css_new_path = __dirname+"/newFile/page/css/index.css"
const dialog_new_path = __dirname+"/newFile/page/comp/Dialog.jsx"
const search_new_path = __dirname+"/newFile/page/comp/SearchForm.jsx"
const data_new_path = __dirname+"/newFile/page/data.js"
const index_new_path = __dirname+"/newFile/page/index.jsx"


const mysql = require('mysql');
const conn = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'q48701866',
    database: 'crowdfunding',
    port: 3306
});
conn.connect();


const creatApi = () => {
    const res = fs.readFileSync(api_temp_path, 'utf-8')
    const fileInfo = res.replaceAll('${name}', name).replaceAll('${lowerName}', lowerName).replaceAll('${name_CN}',name_CN)
    fs.writeFileSync(api_new_path, fileInfo)
    console.log("创建Api文件成功")
}

const creatStore = () => {
    const res = fs.readFileSync(store_temp_path, 'utf-8')
    const fileInfo = res.replaceAll('${name}', name).replaceAll('${lowerName}', lowerName).replaceAll('${name_CN}',name_CN)
    fs.writeFileSync(store_new_path, fileInfo)
    console.log("创建store文件成功")
}

const creatCss = () => {
    fs.writeFileSync(css_new_path, "")
    console.log("创建CSS文件成功")
}

const createDiaLog = () => {
    conn.query(`select * from ${lowerName}`, function (err, result, fields) {
        const fieldsList = fields.map(item => item.name)
        const rules = fieldsList.map(item => {
            return `
               ${item}: [
                    {required: true, message: '请输入', trigger: 'blur'},
                ]
            `
        })
        const formItem = fieldsList.map(item => {
            return `<el-form-item label={"${item}"} prop={'${item}'}>
                 <el-input v-model={props.diaForm.${item}}/>
              </el-form-item>`
        }).join(" ")
        const res = fs.readFileSync(dialog_temp_path, 'utf-8')
        const fileInfo = res.replaceAll('${name}', name).replaceAll('${lowerName}', lowerName).
        replaceAll('${rules}', rules).replaceAll('${formItem}', formItem).replaceAll('${name_CN}',name_CN)
        fs.writeFileSync(dialog_new_path, fileInfo)
        console.log("生成dia成功")
    })
}

const createSearch = () => {
    const res = fs.readFileSync(search_temp_path, 'utf-8')
    const fileInfo = res.replaceAll('${name}', name).replaceAll('${lowerName}', lowerName).replaceAll('${name_CN}',name_CN)
    fs.writeFileSync(search_new_path, fileInfo)
    console.log('创建search文件成功')
}

const createData = () => {
    conn.query(`select * from ${lowerName}`, function (err, result, fields) {
        const fieldsList = fields.map(item => item.name)
        const dataItem = fieldsList.map(item => {
            return ` {
                    label: "${item}",
                    prop: '${item}',
                    align: 'center'
                }`
        })
        const res = fs.readFileSync(data_temp_path, 'utf-8')
        const fileInfo = res.replaceAll('${name}', name).replaceAll('${lowerName}', lowerName).replaceAll('${name_CN}',name_CN)
        .replaceAll('${header}', dataItem)
        fs.writeFileSync(data_new_path, fileInfo)
        console.log("生成Data成功")
    })
}

const creatIndex = () => {
    const res = fs.readFileSync(index_temp_path, 'utf-8')
    const fileInfo = res.replaceAll('${name}', name).replaceAll('${lowerName}', lowerName).replaceAll('${name_CN}',name_CN)
    fs.writeFileSync(index_new_path, fileInfo)
    console.log("创建index文件成功")
}
creatApi()
creatStore()
creatCss()
createDiaLog()
createSearch()
createData()
creatIndex()
